#include "ring_queue.hpp"
#include "task.hpp"

RingQueue<Task> rq;

void* Produce(void*)
{
  pthread_detach(pthread_self());
  char ops[] = "+-*/%";
  while(true){
    int x = rand() % 99 + 1;
    int y = rand() % 99 + 1;
    rq.Push(Task(x, y, ops[rand() % 5]));
    sleep(2);
  } 
}

void* Consume(void* arg)
{
  long i = (long)arg;
  while(true){
    Task t;
    rq.Pop(t);
    printf("thread[%ld]", i);
    t.Run();
    sleep(1);
  }

}

int main()
{
  srand((unsigned long)time(nullptr));
  pthread_t tds[5];
  for(long i = 0; i < 5; i++){
    pthread_create(&tds[i], nullptr, Consume, (void*)i);
  }

  pthread_t tid;
  for(long i = 0; i < 5; i++){
    pthread_create(&tid, nullptr, Produce, nullptr);
  }

  for(int i = 0; i < 5; i++){
    pthread_join(tds[i], nullptr);
  }
  return 0;
}
